import { _decorator, Component, EventKeyboard, Input, input, KeyCode, Node, Quat, RigidBody, Vec3 } from 'cc';
import { TankType } from '../Enum/TankType';
const { ccclass, property } = _decorator;

@ccclass('TankMovement')
export class TankMovement extends Component {
    @property({ tooltip: "Used to identify which tank belongs to which player. This is set by this tank's manager." })
    public playerNumber: number = 1;
    @property({ tooltip: "How fast the tank turns in degrees per second." })
    public turnSpeed: number = 180;
    @property({ tooltip: "How fast the tank moves forward and back." })
    public speed: number = 0.1;
    @property(RigidBody)
    public rigidbody: RigidBody | null = null;
    public tankType: TankType = TankType.Player;
    private movementInputValue: number = 0;
    private turnInputValue: number = 0;

    private fowardKey: KeyCode;
    private backwardKey: KeyCode;
    private leftKey: KeyCode;
    private rightKey: KeyCode;

    onLoad() {
        this.movementInputValue = 0;
        this.turnInputValue = 0;
        if (this.tankType == TankType.Player) {
            input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
            input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
        }
        this.rigidbody.linearDamping = 0.5;
        this.rigidbody.angularDamping = 0.5;
        this.rigidbody.sleepThreshold = 10;

    }

    onDestroy() {
        if (this.tankType == TankType.Player) {
            input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
            input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
        }
    }

    onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case this.fowardKey:
            case this.backwardKey:
                this.movementInputValue = 0;
                break;
            case this.leftKey:
            case this.rightKey:
                this.turnInputValue = 0;
                break;
        }
    }

    onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case this.fowardKey:
                this.movementInputValue = -1;
                break;
            case this.backwardKey:
                this.movementInputValue = 1;
                break;
            case this.leftKey:
                this.turnInputValue = 1;
                break;
            case this.rightKey:
                this.turnInputValue = -1;
                break;
        }
    }

    update(deltaTime: number) {
        if (this.movementInputValue != 0) {
            this.move(deltaTime);
        }
        if (this.turnInputValue != 0) {
            this.rotate(deltaTime);
        }
    }
    private move(dt: number) {
        if (!this.rigidbody) return;

        // 计算新位置
        const currentPosition = this.node.getWorldPosition();
        const newPosition = new Vec3();

        // 根据前进方向、速度、输入值和时间增量计算位移
        Vec3.scaleAndAdd(
            newPosition,
            currentPosition,
            this.node.forward,
            this.speed * dt * this.movementInputValue
        );

        // 直接设置节点位置
        this.node.setWorldPosition(newPosition);

    }

    public bindKey(fKey: KeyCode, bKey: KeyCode, lKey: KeyCode, rKey: KeyCode) {
        this.fowardKey = fKey;
        this.backwardKey = bKey;
        this.leftKey = lKey;
        this.rightKey = rKey;
    }

    private rotate(dt: number) {
        if (!this.rigidbody) return;

        // 计算新旋转
        const currentRotation = this.node.getRotation();
        const newRotation = new Quat();

        // 创建一个绕Y轴的旋转四元数
        const rotationQuat = new Quat();
        Quat.fromAxisAngle(
            rotationQuat,
            Vec3.UP, // Y轴旋转
            this.turnSpeed * dt * this.turnInputValue
        );

        // 将当前旋转与新旋转相乘
        Quat.multiply(newRotation, currentRotation, rotationQuat);

        // 应用新的旋转
        this.node.setRotation(newRotation);
    }
}

